package com.cdtu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdtu.entity.Category;
import com.cdtu.entity.ResourceType;
import com.cdtu.service.ResourceTypeService;
import com.cdtu.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Sakura
 * @Date : 2022/12/31 21:49
 */
@RestController
@RequestMapping("/category")
//解决跨域问题
@CrossOrigin
public class CategoryController {
    @Autowired
    private ResourceTypeService resourceTypeService;

    @GetMapping("/findAll")
    public List<ResourceType> queryResource(){
        List<ResourceType> list = new ArrayList<>();
        list.add(new ResourceType(1L,"全部"));
        List<ResourceType> resourceTypeList = resourceTypeService.list();
       list.addAll(resourceTypeList);
        return list;
    }
    @GetMapping("/findByPage/{pageNow}/{pageSize}")
    public R getResourceTypeList(@PathVariable("pageNow") int pageNow,
                                 @PathVariable("pageSize") int pageSize){
        Page<ResourceType> typePage = resourceTypeService.page(new Page<>(pageNow, pageSize));
        return R.success(typePage);
    }
    @PostMapping("/addType")
    public R addResourceType(@RequestBody ResourceType type){
//        ResourceType typePage = new ResourceType();
//        typePage.setName(name);
        boolean saveOrUpdate = resourceTypeService.saveOrUpdate(type);
        if (!saveOrUpdate){
            if (type.getId()==0){
                return R.error("添加失败，请重新尝试");
            }else {
                return R.error("修改失败，请重新尝试");
            }
        }

        if (type.getId()==0){
            return R.error("添加成功");
        }else {
            return R.error("修改成功");
        }
    }
    @PostMapping("/delete/{tid}")
    public R deleteType(@PathVariable("tid") long tid){
        boolean remove = resourceTypeService.removeById(tid);
        if (!remove){
            return R.error("删除失败，请重新尝试");
        }
        return R.success("删除成功");
    }


}
